package xyz.jangle.thread.storage;

import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

public class Storage {
	
	private final int max_size;
	
	private Queue<Date> queue;

	public Storage() {
		super();
		max_size = 10;
		queue = new LinkedList<Date>();
	}
	
	
	public synchronized void set() {
		while(queue.size()==max_size) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Date date = new Date();
		System.out.println("Set:"+date);
		queue.offer(date);
		notify();
	}
	
	public synchronized void get() {
		while(queue.size()==0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Date poll = queue.poll();
		System.out.println("Get:"+poll);
		notify();
	}
	
	

}
